package com.njtoyo.taxi.admin.handler.common;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.njtoyo.taxi.admin.library.common.Enum;
import com.njtoyo.taxi.admin.rest.presenter.data.chart.RankChartItemPresenter;
import lombok.Data;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Data
public class DateIntervalHandler {
    private DateTime startDate;
    private DateTime endDate;
    private Enum.DateGroupType type;

    public DateIntervalHandler(Enum.DateGroupType type, Long interval, Date customizeStartTime, Date customizeEndTime) {
        this.type = type;
        DateTime now = new DateTime();
        switch (type) {
            case customize:
                // 自定义
                this.type = Enum.DateGroupType.month;
                int intervalMonth = intervalMonth(customizeStartTime, customizeEndTime);

                // 间隔小于4个月
                if (intervalMonth < 4) {
                    // 和‘本月’逻辑相同
                    startDate = DateUtil.beginOfMonth(DateUtil.offsetMonth(customizeEndTime, (int) -interval));
                } else {
                    // 指定超过3个月份时，开始时间为指定开始时间的前一个月的月初；结束时间为结束时间的月末；
                    startDate = DateUtil.beginOfMonth(DateUtil.offsetMonth(customizeStartTime, -1));
                }
                endDate = DateUtil.endOfMonth(customizeEndTime);

                break;
            case day:
                endDate = DateUtil.endOfDay(DateUtil.yesterday());
                startDate = DateUtil.beginOfDay(DateUtil.offsetDay(endDate, (int) -interval + 1));
                break;
            case week:
                endDate = DateUtil.offsetWeek(DateUtil.endOfWeek(now), -1);
                startDate = DateUtil.offsetWeek(DateUtil.beginOfWeek(now), (int) -interval);
                break;
            case month:
                endDate = DateUtil.offsetMonth(DateUtil.endOfMonth(now), -1);
                startDate = DateUtil.offsetMonth(DateUtil.beginOfMonth(now), (int) -interval);
                break;
        }
    }

    public List<RankChartItemPresenter> getDateList() {
        List<RankChartItemPresenter> list = new ArrayList<>();
        DateTime start = this.startDate;
        DateTime end = this.endDate;

        while (end.after(start)) {
            RankChartItemPresenter item = new RankChartItemPresenter();
            item.setStart(start);
            switch (this.type) {
                case day:
                    item.setEnd(DateUtil.endOfDay(start));
                    start = DateUtil.offsetDay(start, 1);
                    break;
                case week:
                    item.setEnd(DateUtil.endOfWeek(start));
                    start = DateUtil.offsetWeek(start, 1);
                    break;
                case month:
                    item.setEnd(DateUtil.endOfMonth(start));
                    start = DateUtil.offsetMonth(start, 1);
                    break;
            }
            list.add(item);
        }
        return list;
    }

    public static int intervalMonth(Date startTime, Date endTime) {
        Calendar bef = Calendar.getInstance();
        Calendar aft = Calendar.getInstance();
        bef.setTime(startTime);
        aft.setTime(endTime);
        int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
        int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;

        return Math.abs(month + result);
    }
}
